<?php

declare(strict_types=1);

namespace App\Service\Logic;

use App\Service\Business\UserBusiness;
use App\Validator\CommonValidator;
use App\Validator\UserValidator;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 用户-逻辑类
 */
class UserLogic extends BaseLogic
{
        #[Inject]
        protected UserBusiness $UserBusiness;

        #[Inject]
        protected UserValidator $UserValidator;

        #[Inject]
        protected CommonValidator $CommonValidator;

        /**
         * 用户名登录
         * @return array
         * @throws Exception
         */
        public function loginUsername(): array
        {
                $this->UserValidator->validate($this->request->all(), ['username', 'password']);
                return $this->UserBusiness->loginUsername();
        }

        /**
         * 获取当前登录者信息
         * @return array
         */
        public function info(): array
        {
                return $this->UserBusiness->info();
        }

        /**
         * 分页
         * @return array
         * @throws Exception
         */
        public function paginate(): array
        {
                $this->CommonValidator->validate($this->request->all(), ['page', 'limit']);
                return $this->UserBusiness->paginate();
        }

        /**
         * 创建
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function create(): array
        {
                $this->UserValidator->validate($this->request->all(), ['name', 'mobile', 'username', 'password', 'status']);
                $this->UserValidator->uniqueFieldMobile($this->request->all(), 0);
                $this->UserValidator->uniqueFieldUsername($this->request->all(), 0);
                return $this->UserBusiness->create();
        }

        /**
         * 更新
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function update(): array
        {
                $this->UserValidator->validate($this->request->all(), ['userId', 'name', 'username', 'status']);
                $this->UserValidator->uniqueFieldUsername($this->request->all(), $this->request->input('userId'));
                if (intval($this->request->input('userId')) === 1 && intval($this->request->getAttribute('userId')) !== 1) {
                        throw new Exception(trans('user.tip.prohibitOperateAdministrator'));
                }
                return $this->UserBusiness->update();
        }

        /**
         * 更新密码
         * @return array
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         * @throws Exception
         */
        public function updatePassword(): array
        {
                $this->UserValidator->validate($this->request->all(), ['userId', 'password']);
                if (intval($this->request->input('userId')) === 1 && intval($this->request->getAttribute('userId')) !== 1) {
                        throw new Exception(trans('user.tip.prohibitOperateAdministrator'));
                }
                return $this->UserBusiness->updatePassword();
        }
}